package com.quanyan.club.web.admin;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.club.entity.admin.ClubActivityDetail;
import com.quanyan.club.entity.admin.ClubActivityListDo;
import com.quanyan.club.entity.admin.ReqClubActivityAdmin;
import com.quanyan.club.entity.vo.req.ReqAgreeJoinClub;
import com.quanyan.club.entity.vo.req.ReqClubMultimediaPage;
import com.quanyan.club.entity.vo.req.admin.*;
import com.quanyan.club.entity.vo.resp.RespClubMemberInfo;
import com.quanyan.club.entity.vo.resp.RespClubMultimedia;
import com.quanyan.club.entity.vo.resp.admin.RespClubGameOrder;
import com.quanyan.club.service.*;
import com.quanyan.club.web.BaseController;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.statium.api.resp.RespGameOrder;
import com.quanyan.statium.service.StadiumOpenService;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by zhuo.shi on 2016/8/19.
 */
@RestController
@RequestMapping(value = "/admin/api/club")
public class ClubAdminController extends  BaseController{

    @Autowired
    private ClubAdminMgmService clubAdminMgmService;
    @Autowired
    private ClubDataService clubDataService;
    @Autowired
    private StadiumOpenService stadiumOpenService;
    @Autowired
    private ClubMemberService clubMemberService;
    @Autowired
    private ClubMultimediaService clubMultimediaService;
    @Autowired
    private ClubActivityAdminService clubActivityAdminService;
    @Autowired
    private ClubTaskEggService clubTaskEggService;
    @Autowired
    private ClubIntegralService clubIntegralService;

    //重新计算俱乐部周任务积分
    @RequestMapping(value = "/reindexClubWeekTaskIntegral")
    public APIResponse reindexClubWeekTaskIntegral(){
        clubIntegralService.reindexClubWeekTaskIntegral();
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/loadClub")
    public APIResponse loadClub(){
        clubDataService.loadClubFromV1();
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/directModifyClubWeekTaskStar")
    public APIResponse directModifyClubWeekTaskStar(@RequestBody Map map){
        clubTaskEggService.directModifyClubWeekTaskStar(new Date(MapUtils.getLong(map, "startTime")), new Date(MapUtils.getLong(map, "endTime")));
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/loadClubMember")
    public APIResponse loadClubMember(){
        clubDataService.loadClubMemberFromV1();
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/loadClubMemberIM")
    public APIResponse loadClubMemberIM(){
        clubDataService.loadClubMemberIM();
        return APIResponse.returnSuccess();
    }
    @RequestMapping(value = "/loadClubMemberIM2")
    public APIResponse loadClubMemberIM2(){
        clubDataService.loadClubMemberIM2();
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/loadClubMemberIndex", method = RequestMethod.GET)
    public APIResponse loadClubMemberIndex(int clubId){
        clubMemberService.updateMemberIndexOfClub(clubId);
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/loadClubIndex", method = RequestMethod.GET)
    public APIResponse loadClubIndex(int clubId){
        clubAdminMgmService.reIndexClub(clubId);
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/loadClubIndexAll")
    public APIResponse loadClubIndexAll(){
        clubAdminMgmService.loadClubIndexAll();
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/loadActivityIndexAll")
    public APIResponse loadActivityIndexAll(){
        clubAdminMgmService.loadActivityIndexAll();
        return APIResponse.returnSuccess();
    }

    @RequestMapping(value = "/queryClubList", method = RequestMethod.POST)
    public APIResponse queryClubList(@RequestBody ReqClubQuery reqClubQuery){
        return APIResponse.returnSuccess(clubAdminMgmService.queryClubList(reqClubQuery));
    }
    @RequestMapping(value = "/queryClubDetails", method = RequestMethod.POST)
    public APIResponse queryClubDetails(@RequestBody Map map){
        Date trendStartTime = (Date) MapUtils.getObject(map,"trendStartTime");
        Date trendEndTime = (Date)MapUtils.getObject(map,"trendEndTime");
        return APIResponse.returnSuccess(clubAdminMgmService.queryClubDetails(trendStartTime,trendEndTime));
    }

    @RequestMapping(value = "/clubInfo", method = RequestMethod.POST)
    public APIResponse getClubInfo(@RequestBody Map map){
        Integer clubId = MapUtils.getInteger(map, "clubId");
        if (clubId == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(clubAdminMgmService.getClubInfo(clubId));
    }

    @RequestMapping(value = "/updateClubInfo", method = RequestMethod.POST)
    public APIResponse updateClubInfo(@RequestBody ReqClubQuery reqClubQuery){
        Integer clubId = reqClubQuery.getClubId();
        if (clubId == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(clubAdminMgmService.updateClubInfo(reqClubQuery));
    }

    /**
     * 俱乐部相册列表
     * @param reqClubMultimediaPage
     * @return
     */
    @RequestMapping("/list")
    public APIResponse<PageObj<List<RespClubMultimedia>>> list(@Valid @RequestBody ReqClubMultimediaPage reqClubMultimediaPage){
        PageObj<List<RespClubMultimedia>> pageObj = this.clubMultimediaService.list(reqClubMultimediaPage);
        return APIResponse.returnSuccess(pageObj);
    }

    /**
     * 添加俱乐部相册
     * @param reqMultimediaAdd
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public APIResponse add(@Valid @RequestBody ReqMultimediaAdd reqMultimediaAdd,HttpServletRequest request){
        int uid = getUid(request,false);
        boolean flag = clubAdminMgmService.add(reqMultimediaAdd,uid);
        if(flag){
            return APIResponse.returnSuccess();
        }else{
            return APIResponse.returnFail("上传失败,请重新上传！");
        }
    }
    @RequestMapping("/delete")
    public APIResponse delete(@RequestBody Map map){
        Integer multimediaId = MapUtils.getInteger(map, "multimediaId");
        int flag = this.clubMultimediaService.delete(multimediaId);
        if(flag>0){
            return APIResponse.returnSuccess();
        }else{
            return APIResponse.returnFail("删除失败,请重新操作！");
        }
    }
    @RequestMapping(value = "/clubActivityDetailList", method = RequestMethod.POST)
    public String getClubActivityDetailList(@RequestBody ReqClubActivityAdmin req){
        PageObj<List<ClubActivityListDo>> result = clubActivityAdminService.queryClubActivityList(req);
        if (null == result)
        {
            return JSONObject.toJSONString(APIResponse.returnFail("查询失败"));
        }
        return JSONObject.toJSONString(APIResponse.returnSuccess(result),
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }
    @RequestMapping(value = "/clubActivityDetail", method = RequestMethod.GET)
    public String selectClubActivityDetail(Integer activityId)
    {
        ClubActivityDetail clubActivityDetail =
                clubActivityAdminService.getClubActivityDetail(activityId);
        if (null == clubActivityDetail)
        {
            return JSONObject.toJSONString(APIResponse.returnFail("查询失败"));
        }
        return JSONObject.toJSONString(APIResponse.returnSuccess(clubActivityDetail),
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullStringAsEmpty);
    }

    @RequestMapping(value = "/clubMemberList")
    public APIResponse getClubMemberList(@RequestBody Map map){
        Integer clubId = MapUtils.getInteger(map, "clubId");
        Integer pageNo = MapUtils.getInteger(map, "pageNo");
        Integer pageSize = MapUtils.getInteger(map, "pageSize");
        boolean isTeam = MapUtils.getBooleanValue(map, "isTeamMember");
        if (clubId == null || pageNo == null ||
                pageNo < 0 || pageSize == null || pageSize < 0){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(
                clubAdminMgmService.queryClubMemberList(clubId, isTeam, pageNo, pageSize));
    }

    @RequestMapping(value = "/updateClubMember")
    public APIResponse updateClubMember(@RequestBody RespClubMemberInfo info){
        return clubAdminMgmService.updateClubMember(info);
    }

    @RequestMapping(value = "/sysLogoList")
    public APIResponse logoList(@RequestBody Map map){
        Integer sportTypeId = MapUtils.getInteger(map, "sportTypeId");
        Integer pageNo = MapUtils.getInteger(map, "pageNo");
        Integer pageSize = MapUtils.getInteger(map, "pageSize");
        if (sportTypeId == null || pageNo == null ||
                pageNo < 0 || pageSize == null || pageSize < 0){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(
                clubAdminMgmService.queryClubSystemLogo(sportTypeId, pageNo, pageSize));
    }

    @RequestMapping(value = "/deleteMember")
    public APIResponse deleteMember(@RequestBody ReqAgreeJoinClub reqAgreeJoinClub){
//        Integer clubId = MapUtils.getInteger(map, "clubId");
//        Integer uid = MapUtils.getInteger(map, "uid");
        Integer clubId = reqAgreeJoinClub.getClubId();
        Integer uid = reqAgreeJoinClub.getUid();
        int larkAppId = reqAgreeJoinClub.getLarkAppId();
        if (clubId == null || uid == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return clubAdminMgmService.deleteClubMember(clubId, uid, larkAppId);
    }

    @RequestMapping(value = "/autoPromptingClubs")
    public APIResponse autoPrompting(@RequestBody Map map){
        return APIResponse.returnSuccess(
                clubAdminMgmService.autoPrompting(MapUtils.getString(map, "clubName")));
    }

    @RequestMapping(value = "/memberFinanceStreamList")
    public APIResponse clubMemberFinanceStreamList(@RequestBody ReqClubFinanceQuery query){
        return APIResponse.returnSuccess(
                clubAdminMgmService.queryClubMemberFinaceStream(query));
    }

    @RequestMapping(value = "/withdrawRecordList")
    public APIResponse queryWithdrawRecordList(@RequestBody ReqClubFinanceQuery query){
        if (query.getPageNo() == null
                || query.getPageSize() == null){
            return APIResponse.returnFail(ApiResponseEnum.PARAMETER_CANT_BE_EMPTY);
        }
        return APIResponse.returnSuccess(
                clubAdminMgmService.queryWithdrawRecord(query));
    }

    @RequestMapping(value = "/activityFinanceStreamList")
    public APIResponse queryActivityFinanceStreamList(@RequestBody ReqClubFinanceQuery query){
        return APIResponse.returnSuccess(
                clubAdminMgmService.queryClubActFinaceStream(query));
    }

    @RequestMapping(value = "/financeReport")
    public APIResponse queryClubFinanceReport(@RequestBody ReqClubFinanceQuery query){

        /*return APIResponse.returnSuccess(
                clubAdminMgmService.queryClubFinanceReport(query));*/
        return APIResponse.returnFail("功能关闭，请联系后台管理员");
    }

    @RequestMapping(value = "/clubRechargeStream")
    public APIResponse queryClubRechargeStream(@RequestBody ReqClubRechargeQuery query){

        return APIResponse.returnSuccess(clubAdminMgmService.queryClubRechargeStream(query));
    }

    @RequestMapping(value = "/queryClubRechargeStreamReport")
    public APIResponse queryClubRechargeStreamReport(@RequestBody ReqClubRechargeQuery query) {
        return APIResponse.returnSuccess(clubAdminMgmService.queryClubRechargeStreamReport(query));
    }

    @RequestMapping(value = "/clubFinanceSummaryList")
    public APIResponse clubFinanceSummaryList(@RequestBody ReqClubFinanceReconciliationQuery query){

        return APIResponse.returnSuccess(
                clubAdminMgmService.queryClubFinanceReconciliationList(query));
    }

    @RequestMapping(value = "/clubFinanceSummaryExport")
    public APIResponse clubFinanceSummaryExport(@RequestBody ReqClubFinanceReconciliationQuery query){
        return APIResponse.returnSuccess(
                clubAdminMgmService.exportClubFinanceReconciliationList(query));
    }

    @RequestMapping(value = "/clubPaymentDetailList")
    public APIResponse clubPaymentList(@RequestBody ReqClubPaymentQuery query){

        return APIResponse.returnSuccess(
                clubAdminMgmService.queryClubPaymentDetailsList(query));
    }

    @RequestMapping(value = "/clubActivityOrderDetailList")
    public APIResponse clubActivityOrderDetailList(@RequestBody Map map){
        Integer activityId=MapUtils.getInteger(map, "activityId");
        Integer pageNo = MapUtils.getInteger(map, "pageNo");
        Integer pageSize = MapUtils.getInteger(map, "pageSize");
        return APIResponse.returnSuccess(
                clubAdminMgmService.queryActivityOrderList(activityId,pageNo,pageSize));
    }

    @RequestMapping(value = "/clubGameOrderDetailList")
    public APIResponse clubGameOrderDetailList(@RequestBody Map map){

        List<RespGameOrder> list = stadiumOpenService.queryOrderNoByGameId(MapUtils.getInteger(map, "gameId"));
        List<RespClubGameOrder> resultList = new ArrayList<>();
        if (list != null && list.size()>0){
            for (RespGameOrder gameOrder : list){
                RespClubGameOrder clubGameOrder = new RespClubGameOrder();
                clubGameOrder.setAccumulate(new BigDecimal(gameOrder.getAccumulate()).divide(new BigDecimal(10), 2, RoundingMode.HALF_UP).doubleValue());
                clubGameOrder.setGameOrderNo(gameOrder.getGameOrderNo());
                clubGameOrder.setPayAmount(gameOrder.getPayAmount());
                clubGameOrder.setPayTime(gameOrder.getPayTime());
                clubGameOrder.setPayType(gameOrder.getPayType());
                clubGameOrder.setTradeOrderNo(gameOrder.getTradeOrderNo());
                clubGameOrder.setPrice(gameOrder.getPrice());
                clubGameOrder.setStatusCode(0);
                resultList.add(clubGameOrder);
            }
        }
        return APIResponse.returnSuccess(resultList);
    }
}
